package com.guoq.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.guoq.pojo.User;
import com.guoq.service.IUserService;


@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private IUserService userService;
	
	@RequestMapping("/showUser")
	public String toIndex(HttpServletRequest request,Model model,ModelMap mdmap){
		int userId = Integer.parseInt(request.getParameter("id"));
		User user = this.userService.getUserById(userId);
//		model.addAttribute("user", JSON.toJSONString(user));
		mdmap.addAttribute("user1", JSON.toJSONString(user));
		return "showUser";
	}
	
//	@RequestMapping(value="/showUser",method = RequestMethod.GET)
//	public User selectUserById() throws Exception{
//		User user = this.userService.getUserById(1);
//		return user;
//	}
	
	@RequestMapping(value="/userList",method = RequestMethod.GET)
	public ModelAndView userList() throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		List<User> list= userService.getUserAll();
		modelAndView.addObject("user",list);
		modelAndView.setViewName("showUser");
		return modelAndView;
	}
	
	@RequestMapping("/delUser")
	public String delUser(HttpServletRequest request,Model model,@RequestParam(value="id",required=true) Integer id){
		System.out.println("id:" + id);
		int i = this.userService.deleteByPrimaryKey(id);
		if(i==1){
			model.addAttribute("user_msg", "删除成功");
		}
		return "showUser";
	}
	
	@RequestMapping("/insertUser")
	public String insertUser(HttpServletRequest request,Model model,@RequestParam(value="id",required=true) Integer id){
		System.out.println("id:" + id);
		User user = userService.getUserById(1);
		int i = this.userService.insertSelective(user);
		if(i==1){
			model.addAttribute("user_msg", "新增成功");
		}
		return "showUser";
	}
	
	@RequestMapping(value="/getUserById",method = RequestMethod.GET)
	@ResponseBody
	public String getUserById() throws Exception{
		User user = userService.getUserById(1);
		return JSON.toJSONString(user);
	}
	
	@RequestMapping("/getRequestInfo")
	public String getRequestInfo(@RequestHeader("Accept-Language") String language,
			@CookieValue("JSESSIONID") String jSessionId,Model model){
		System.out.println("language:" + language);
		System.out.println("jSessionId:" + jSessionId);
//		model.addAttribute("user_msg", jSessionId);
		return "showUser";
	}
	
	
	
}
 